package com.sskj.gcc.ui.fragment;


import android.content.Intent;
import android.icu.math.BigDecimal;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.sskj.common.rxbus2.RxBus;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.KeyBoardUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.gcc.R;
import com.sskj.gcc.bean.game.GameExchangeParametersBean;
import com.sskj.gcc.presenter.game.GameFragmentPresenter;
import com.sskj.gcc.ui.dialog.GameBottomDialog;
import com.sskj.gocoin.common.Constants;
import com.sskj.gocoin.common.MoneyValueFilter;
import com.sskj.gocoin.utils.NumberUtil;
import com.sskj.gocoin.v.activity.WebActivity;
import com.sskj.lib.RxBusCode;

import java.text.DecimalFormat;

import butterknife.BindView;

public class GameBottomExchangeFragment extends GameBaseFragment {

    @BindView(R.id.rb_buy_egg)
    RadioButton rbBuyEgg;
    @BindView(R.id.rb_exchange_food)
    RadioButton rbExchangeFood;
    @BindView(R.id.rb_exchange_bird)
    RadioButton rbExchangeBird;
    @BindView(R.id.rb_exchange_egg)
    RadioButton rbExchangeEgg;
    @BindView(R.id.et_input_count)
    EditText etInputCount;
    @BindView(R.id.tv_useable_usdt)
    TextView tvUseableUsdt;
    @BindView(R.id.tv_need_usdt)
    TextView tvNeedUsdt;
    @BindView(R.id.et_input_password)
    EditText etInputPassword;
    @BindView(R.id.cb_protocol)
    CheckBox cbProtocol;
    @BindView(R.id.tvAgree_protocol)
    TextView tvAgreeProtocol;
    @BindView(R.id.ll_protocol)
    LinearLayout llProtocol;
    @BindView(R.id.btn_sure)
    Button btnSure;
    @BindView(R.id.rg_title)
    RadioGroup rgTitle;
    private GameExchangeParametersBean gameExchangeParametersBean;
    private String need_number_USDT;
    private String can_change_number_food;
    private String can_change_number_bird;
    private String can_change_number_eggs;

    @Override
    public int getLayoutId() {
        return R.layout.app_fragment_game_exchange;
    }

    @Override
    public GameFragmentPresenter getPresenter() {
        return new GameFragmentPresenter();
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    @Override
    public void initView() {
        rgTitle.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_buy_egg:
                        etInputCount.setText(null);
                        ((GameBottomDialog) getActivity()).ivBottomLogo.setImageResource(R.drawable.app_home_game_exchange);
                        llProtocol.setVisibility(View.VISIBLE);
                        if (gameExchangeParametersBean != null) {
                            etInputCount.setHint(String.format(getString(R.string.strMainActivityGame00027), gameExchangeParametersBean.getB1().getA3()));
                            tvUseableUsdt.setText(String.format(getString(R.string.strMainActivityGame00028), NumberUtil.keepNoZore(gameExchangeParametersBean.getB1().getA9().getBalance())));
                        }
                        break;
                    case R.id.rb_exchange_bird:
                        etInputCount.setText(null);
                        ((GameBottomDialog) getActivity()).ivBottomLogo.setImageResource(R.drawable.app_home_game_bird);
                        llProtocol.setVisibility(View.GONE);
                        if (gameExchangeParametersBean != null) {
                            etInputCount.setHint(String.format(getString(R.string.strMainActivityGame00029), gameExchangeParametersBean.getB2().getA3()));
                            tvUseableUsdt.setText(String.format(getString(R.string.strMainActivityGame00030), NumberUtil.keepNoZore(gameExchangeParametersBean.getB2().getA9().getBalance())));
                        }
                        break;
                    case R.id.rb_exchange_food:
                        etInputCount.setText(null);
                        ((GameBottomDialog) getActivity()).ivBottomLogo.setImageResource(R.drawable.app_home_game_food);
                        llProtocol.setVisibility(View.GONE);
                        if (gameExchangeParametersBean != null) {
                            etInputCount.setHint(String.format(getString(R.string.strMainActivityGame00029), gameExchangeParametersBean.getB3().getA3()));
                            tvUseableUsdt.setText(String.format(getString(R.string.strMainActivityGame00030), NumberUtil.keepNoZore(gameExchangeParametersBean.getB3().getA9().getBalance())));
                        }
                        break;
                    case R.id.rb_exchange_egg:
                        etInputCount.setText(null);
                        ((GameBottomDialog) getActivity()).ivBottomLogo.setImageResource(R.drawable.app_home_game_exchange);
                        llProtocol.setVisibility(View.GONE);
                        if (gameExchangeParametersBean != null) {
                            etInputCount.setHint(String.format(getString(R.string.strMainActivityGame00029), gameExchangeParametersBean.getB4().getA3()));
                            tvUseableUsdt.setText(String.format(getString(R.string.strMainActivityGame00030), NumberUtil.keepNoZore(gameExchangeParametersBean.getB4().getA9().getBalance())));
                        }
                        break;
                }
            }
        });

        etInputCount.setFilters(new InputFilter[]{new MoneyValueFilter(0)});
        DecimalFormat mFormat = new DecimalFormat("0.0000");

        etInputCount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() > 9) {
                    s.delete(9, s.length());
                }
                if (gameExchangeParametersBean != null && !"0".equals(etInputCount.getText().toString()) && !TextUtils.isEmpty(etInputCount.getText().toString())) {
                    double d;
                    String[] b;
                    int inputNumber = new BigDecimal(etInputCount.getText().toString()).intValue();
                    switch (rgTitle.getCheckedRadioButtonId()) {
                        case R.id.rb_buy_egg:
                            if (gameExchangeParametersBean.getB1() != null) {
                                b = gameExchangeParametersBean.getB1().getA1().split(":");
                                d = Double.parseDouble(b[0]) * inputNumber / Double.parseDouble(b[1]);
                                need_number_USDT = NumberUtil.keepNoZore(mFormat.format(d));
                                tvNeedUsdt.setText(String.format(getString(R.string.strMainActivityGame00031), need_number_USDT));
                            }
                            break;
                        case R.id.rb_exchange_bird:
                            if (gameExchangeParametersBean.getB2() != null) {
                                b = gameExchangeParametersBean.getB2().getA1().split(":");
                                d = Double.parseDouble(b[0]) * inputNumber / Double.parseDouble(b[1]);
                                can_change_number_food = NumberUtil.keepNoZore(mFormat.format(d));
                                tvNeedUsdt.setText(String.format(getString(R.string.strMainActivityGame00032), can_change_number_food));
                            }
                            break;
                        case R.id.rb_exchange_food:
                            if (gameExchangeParametersBean.getB3() != null) {
                                b = gameExchangeParametersBean.getB3().getA1().split(":");
                                d = Double.parseDouble(b[0]) * inputNumber / Double.parseDouble(b[1]);
                                can_change_number_bird = NumberUtil.keepNoZore(mFormat.format(d));
                                tvNeedUsdt.setText(String.format(getString(R.string.strMainActivityGame00032), can_change_number_bird));
                            }
                            break;
                        case R.id.rb_exchange_egg:
                            if (gameExchangeParametersBean.getB4() != null) {
                                b = gameExchangeParametersBean.getB4().getA1().split(":");
                                d = Double.parseDouble(b[0]) * inputNumber / Double.parseDouble(b[1]);
                                can_change_number_eggs = NumberUtil.keepNoZore(mFormat.format(d));
                                tvNeedUsdt.setText(String.format(getString(R.string.strMainActivityGame00033), can_change_number_eggs));
                            }
                            break;
                    }
                } else {
                    //还原初始化样式
                    switch (rgTitle.getCheckedRadioButtonId()) {
                        case R.id.rb_buy_egg:
                            tvNeedUsdt.setText(String.format(getString(R.string.strMainActivityGame00031), "--"));
                            break;
                        case R.id.rb_exchange_food:
                            tvNeedUsdt.setText(String.format(getString(R.string.strMainActivityGame00032), "--"));
                            break;
                        case R.id.rb_exchange_bird:
                            tvNeedUsdt.setText(String.format(getString(R.string.strMainActivityGame00032), "--"));
                            break;
                        case R.id.rb_exchange_egg:
                            tvNeedUsdt.setText(String.format(getString(R.string.strMainActivityGame00033), "--"));
                            break;
                    }
                }
            }
        });

        ClickUtil.click(btnSure, () -> {
            if (rgTitle.getCheckedRadioButtonId() == R.id.rb_exchange_food
                    || rgTitle.getCheckedRadioButtonId() == R.id.rb_exchange_egg) {
                ToastUtil.showShort(getString(R.string.str_temporarily_opened));
                return;
            }
            //某些机型，键盘挡住toast
            KeyBoardUtil.closeKeybord(etInputCount, getActivity());
            KeyBoardUtil.closeKeybord(etInputPassword, getActivity());

            if (TextUtils.isEmpty(etInputCount.getText().toString())) {
                ToastUtil.showShort(etInputCount.getHint().toString());
                return;
            }
            if (TextUtils.isEmpty(etInputPassword.getText().toString())) {
                ToastUtil.showShort(etInputPassword.getHint().toString());
                return;
            }
            if (gameExchangeParametersBean == null) {
                ToastUtil.showShort(getString(R.string.strMainActivityGame00034));
                return;
            }
            int inputNumber = new BigDecimal(etInputCount.getText().toString()).intValue();
            switch (rgTitle.getCheckedRadioButtonId()) {
                case R.id.rb_buy_egg:
                    if (!cbProtocol.isChecked()) {
                        ToastUtil.showShort(getString(R.string.strMainActivityGame00035));
                        return;
                    }
                    if (inputNumber < Integer.parseInt(gameExchangeParametersBean.getB1().getA3())) {
                        ToastUtil.showShort(etInputCount.getHint().toString());
                        return;
                    }
                    if (inputNumber > Integer.parseInt(gameExchangeParametersBean.getB1().getA2())) {
                        ToastUtil.showShort(String.format(getString(R.string.strMainActivityGame00036), gameExchangeParametersBean.getB1().getA2()));
                        return;
                    }
                    mPresenter.commitExchange(gameExchangeParametersBean.getB1().getA11() + "", inputNumber + "", etInputPassword.getText().toString());
                    break;
                case R.id.rb_exchange_food:
                    if (inputNumber < Integer.parseInt(gameExchangeParametersBean.getB3().getA3())) {
                        ToastUtil.showShort(etInputCount.getHint().toString());
                        return;
                    }
                    if (inputNumber > Integer.parseInt(gameExchangeParametersBean.getB3().getA2())) {
                        ToastUtil.showShort(String.format(getString(R.string.strMainActivityGame00037), gameExchangeParametersBean.getB3().getA2()));
                        return;
                    }
                    mPresenter.commitExchange(gameExchangeParametersBean.getB3().getA11() + "", inputNumber + "", etInputPassword.getText().toString());
                    break;
                case R.id.rb_exchange_bird:
                    if (inputNumber < Integer.parseInt(gameExchangeParametersBean.getB2().getA3())) {
                        ToastUtil.showShort(etInputCount.getHint().toString());
                        return;
                    }
                    if (inputNumber > Integer.parseInt(gameExchangeParametersBean.getB2().getA2())) {
                        ToastUtil.showShort(String.format(getString(R.string.strMainActivityGame00037), gameExchangeParametersBean.getB2().getA2()));
                        return;
                    }
                    mPresenter.commitExchange(gameExchangeParametersBean.getB2().getA11() + "", inputNumber + "", etInputPassword.getText().toString());

                    break;
                case R.id.rb_exchange_egg:
                    if (inputNumber < Integer.parseInt(gameExchangeParametersBean.getB4().getA3())) {
                        ToastUtil.showShort(etInputCount.getHint().toString());
                        return;
                    }
                    if (inputNumber > Integer.parseInt(gameExchangeParametersBean.getB4().getA2())) {
                        ToastUtil.showShort(String.format(getString(R.string.strMainActivityGame00038), gameExchangeParametersBean.getB4().getA2()));
                        return;
                    }
                    mPresenter.commitExchange(gameExchangeParametersBean.getB4().getA11() + "", inputNumber + "", etInputPassword.getText().toString());
                    break;
            }
        });
        ClickUtil.click(tvAgreeProtocol, () -> {
            ToastUtil.showShort(getString(R.string.str_temporarily_opened));
//            startActivity(new Intent(getActivity(), WebActivity.class).putExtra(Constants.INTENT_TYPE, "14"));
        });
        mPresenter.getExchangeParameters();

    }

    @Override
    public void onGetExchangeParametersSuccess(GameExchangeParametersBean gameExchangeParametersBean) {
        super.onGetExchangeParametersSuccess(gameExchangeParametersBean);
        this.gameExchangeParametersBean = gameExchangeParametersBean;
        etInputCount.setHint(String.format(getString(R.string.strMainActivityGame00027), gameExchangeParametersBean.getB1().getA3()));
        tvUseableUsdt.setText(String.format(getString(R.string.strMainActivityGame00028), NumberUtil.keepNoZore(gameExchangeParametersBean.getB1().getA9().getBalance())));
    }

    @Override
    public void onCommitExchangeSuccess(String msg) {
        super.onCommitExchangeSuccess(msg);
        RxBus.getDefault().send(RxBusCode.GAME_REFRESH);
        ToastUtil.showShort(msg);
        getActivity().finish();
    }
}


